﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace ThreadSyncron.Utils.TS.Utils.Forms
{
	public partial class FThreadPoolInfo : Form
	{

		private FThreadPoolInfo()
		{
			InitializeComponent();
			this.Visible = false;
			this.txbThreadListInfo.Text = "";
			this.TopMost = true;
		}

		public void Init()
		{}

		#region Delegates & Events

		public delegate void UpdateTextCallback(string text); 
		
		#endregion

		#region Singleton Thread safe

		static private FThreadPoolInfo instance = null;
		static volatile object padlock = new object();

		public static FThreadPoolInfo Instance
		{
			get
			{
				if (instance == null)
					lock (padlock)
					{
						if (instance == null)
							instance = new FThreadPoolInfo();
					}
				return instance;
			}
		}

		public void ShowInfoForm()
		{
			if (this.InvokeRequired)
			{
				this.BeginInvoke(new MethodInvoker(this.ShowInfoForm));
			}
			else
			{
				this.Visible = true;
			}
		}

		public void HideInfoForm()
		{
			if (this.InvokeRequired)
			{
				this.BeginInvoke(new MethodInvoker(this.HideInfoForm));
			}
			else
			{
				this.Visible = false;
			}
		}

		public void AddText(string s)
		{
			if (this.InvokeRequired)
			{
				this.BeginInvoke(new UpdateTextCallback(AddText), new object[] { s });
			}
			else
			{
				this.txbThreadListInfo.Text += (txbThreadListInfo.Text.Length < 2) ? s : Environment.NewLine + s;
				txbThreadListInfo.SelectionStart = txbThreadListInfo.Text.Length;
				txbThreadListInfo.ScrollToCaret();
			}
		}

		#endregion

	}
}
